package com.hjy.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.R;
import com.hjy.product.valid.AddGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjy.product.entity.CategoryEntity;
import com.hjy.product.service.CategoryService;

import javax.validation.Valid;


/**
 * 商品三级分类
 *
 * @author 何骏扬
 * @email 2402668109@qq.com
 * @date 2022-07-16 01:39:19
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取三级分类属性目录
     * @return
     */
    @RequestMapping("/list/tree")
    public R listTree(){
        List<CategoryEntity> list = categoryService.getCategoryListTree();

        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);


        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated @RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);
        categoryService.updateCascade(category);
        return R.ok();
    }

    @RequestMapping("/update/ids")
    public R updateIds(@RequestBody List<CategoryEntity> categoryEntities){
        categoryService.updateBatchById(categoryEntities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        categoryService.removeIdsByLogic(Arrays.asList(catIds));
        return R.ok();
    }
}
